/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;

import java.util.Date;

/**
 * 云存储工具类
 *
 * @author Charles7c
 * @since 2025/1/15 10:00
 */
public class CloudStorageUtils {

    /**
     * 生成文件名
     *
     * @param originalFileName 原始文件名
     * @return 生成的文件名
     */
    public static String generateFileName(String originalFileName) {
        String extension = FileUtil.extName(originalFileName);
        String datePrefix = DateUtil.format(new Date(), "yyyy/MM/dd");
        String uuid = IdUtil.fastSimpleUUID();
        return String.format("%s/%s.%s", datePrefix, uuid, extension);
    }

    /**
     * 从URL中提取文件名
     *
     * @param fileUrl 文件URL
     * @return 文件名
     */
    public static String extractFileNameFromUrl(String fileUrl) {
        if (fileUrl == null || fileUrl.isEmpty()) {
            return null;
        }
        int lastSlashIndex = fileUrl.lastIndexOf('/');
        if (lastSlashIndex != -1 && lastSlashIndex < fileUrl.length() - 1) {
            return fileUrl.substring(lastSlashIndex + 1);
        }
        return fileUrl;
    }

    private CloudStorageUtils() {
    }
}